<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>欢迎查看你的队伍</title>
  <script src="https://cdn.jsdelivr.net/npm/babel-polyfill/dist/polyfill.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/jquery"></script>
  <script src="__PUBLIC__/script/common.js" common-data-1="{:U('Mix/dest')}"></script>
  <link rel="stylesheet" href="__PUBLIC__/script/jquery-weui/dist/lib/weui.min.css">
  <link rel="stylesheet" href="__PUBLIC__/script/jquery-weui/dist/css/jquery-weui.min.css">
  <link rel="stylesheet" href="__PUBLIC__/styles/base.css">
  <link rel="stylesheet" href="__PUBLIC__/styles/my_pingu.css?v=1.0.0">
  <link rel="stylesheet" href="__PUBLIC__/styles/font/iconfont.css">
</head>
<body>
  <!-- 欢迎 -->
  <div class="hidden">
      <input type="hidden" id='ajax_state_chat' name="关停聊天室" value="{:U('chat/ajax_state_chat')}">
    <input type="hidden" name='申请按钮不需要申请时填写' id='formAlreadyHad' value='test'>
    <!-- 不要管下边这一行的表单 -->
    <form autocomplete="off" action method="post" id ='hidden_form'></form>
  </div>
  <div class="container">
    <div class="header">{$Think.session.user.p_u_id|get_web_f_mesage}</div>
    <div class="nav">
      <div class="left" inori-href="{:U('Wchat/main')}" id='my_tuandui'>我的团队</div>
      <div class="center" id='my_pinggu' inori-href="{:U('Mix/show_chat_list')}">我的评估</div>
      <if condition='$Think.session.user.web_type eq 1' ><div id='nav_my_tuandui' class="right active">团队咨询</div></if>
    </div>
    <div class="weui-search-bar" id="searchBar">
      <form autocomplete="off" class="weui-search-bar__form" id='search_for' method="get" action="{:U('Mix/show_chat_list_team')}">
        <div class="weui-search-bar__box">
          <i class="weui-icon-search"></i>
          <input type="search" class="weui-search-bar__input" id="searchInput" placeholder="输入客户姓名、手机号" name='search_kehu_name' required>
          <a href="javascript:" class="weui-icon-clear" id="searchClear"></a>
        </div>
        <label class="weui-search-bar__label" id="searchText">
          <i class="weui-icon-search"></i>
          <span>客户姓名、手机号</span>
        </label>
      </form>
      <a href="javascript:;" class="weui-search-bar__cancel-btn" id="searchCancel">取消</a>
    </div>

    <div class="roomList">
      <!-- 一个聊天室一个roomItem -->
      <!-- roomItem 的inori-href指示跳转目标，不要写a链接了 -->
      <!-- &nbsp; 不能删除，用来占位防止潜在的错位，有些地方我觉得数据是一定存在的所以就不放 -->
      <volist name='apply' id='vo'>
        <div class="roomItem" inori-href='{:U('WebUser/last_product')}?search_data={$vo.search_data}&chat_id={$vo.chat_id}&c_id={$vo.c_id}&&role=0' inori-data='{$vo.chat_id}'>
          <div class="vip_info">
            <div class="vip_name">
              &nbsp;{$vo.kehu_name}
            </div>
            <div class="vip_type">
              &nbsp;{$vo.kehu_old}岁
            </div>
          </div>
          <div class="vip_pro" style="opacity: 0;">
            <div class="vip_pro_qudao">
              &nbsp;
            </div>
            <div class="vip_pro_type js-vip_pro_type" inori-kehu-data='{$vo.data}'>
              <!-- 客户资料 - 额度 -->
              &nbsp;
            </div>
          </div>
          <div class="room_init_time">
            {$vo.time}
          </div>
          <div class="shutdown_this" inori-state='{$vo.state}' inori-state-id='{$vo.chat_id}'>
            <div class="btn_form">关停</div>
          </div>
          <div class="more">
            <i class="iconfont">&#xe6a7;</i>
          </div>
        </div>
      </volist>
    </div>
    <div id='back_all'
      style='text-align: center;font-size: 13px;padding: 15px;text-decoration: underline;color: #757575;'
      inori-href="{:U('Mix/show_chat_list_team')}">点击查看全部
    </div>
  </div>
  <script src="__PUBLIC__/script/jquery-weui/dist/js/jquery-weui.min.js"></script>

  <!-- 跳转 -->
  <script>
    $(document).ready(() => {
      sessionStorage.setItem('kehu_old', '')
      $('#my_tuandui').on('click', (evt) => {
        location.href = `${$(evt.target).attr('inori-href')}?font_end_not_submit=${Math.random()}`
      })
      $('#my_pinggu').on('click', (evt) => {
        location.href = `${$(evt.target).attr('inori-href')}?font_end_not_submit=${Math.random()}`
      })
      // $('#nav_my_tuandui').on('click', (evt) => {
      //   location.href = `${$(evt.target).attr('inori-href')}?font_end_not_submit=${Math.random()}`
      // })
      if (/\?search_kehu_name\=/.test(location.href)) {
        $('#back_all').on('click', (evt) => {
          location.href = `${$(evt.target).attr('inori-href')}?font_end_not_submit=${Math.random()}`
        })
      } else {
        $('#back_all').remove()
      }
      // 跳转聊天室
      $(`.roomItem`).each((index, item) => {
        $(item).on('click', (evt) => {
          const thisRoomItem = $(evt.target).closest(`.roomItem`)
          evt.stopPropagation()
          evt.preventDefault()
          if(/* 关停状态 */ thisRoomItem.find('[inori-state]').length > 0 && thisRoomItem.find('[inori-state]').attr('inori-state') === '0') {
            $.toptip('聊天室处于关停状态', 'warning')
            return
          }
          if(/* 正在跳转 */ window.__jumping_to_chatroom) {
            $.toptip('正在跳转，请耐心等候', 'success')
            return
          }
          window.__jumping_to_chatroom = true
          $.toptip('正在查询聊天室数据，请耐心等候', 'success')
          if(String(thisRoomItem.attr('inori-data')) === '') {
            $.toptip(`该次搜索条件不完整，不予跳转`, 'error')
            throw new Error(`该次搜索条件不完整，不予跳转`)
          }
          if(String(thisRoomItem.attr('inori-href')) !== 'null') {
            thisRoomItem.one('click', () => {
              $.toptip(`正在准备数据，请稍后。。。`, 'success')
            })
            thisRoomItem.one('click', () => {
              location.reload()
            })
            location.href = `${thisRoomItem.attr('inori-href')}?front_end_not_submit=${Math.random()}`
          } else {
            $.toptip(`聊天室链接不存在！请联系相关审核员或者管理员`)
            thisRoomItem.on('click', () => {
              $.toptip(`聊天室链接不存在！请联系相关审核员或者管理员`)
            })
          }
        })
      })
      // 处理日期
      $('.room_init_time').each((index, item) => {
        const realTime = $(item).text().match(/(\d\d\d\d)年(\d\d)月(\d\d)日/)
        if (realTime === null || !realTime.length || realTime.length < 4) {
          console.log(`发现一个不规则的时间展示: ${realTime}`)
        } else {
          $(item).text(`${realTime[1]}/${realTime[2]}/${realTime[3]}`)
        }
      })
      // 处理额度
      $.each($('.js-vip_pro_type'), (index, item) =>{
        let jsonData = $(item).attr('inori-kehu-data')
        try{
          jsonData = JSON.parse($(item).attr('inori-kehu-data'))
          const eduObj = jsonData.find(item => item.name === 'kehu_edu')
          if (eduObj && Number.isFinite(Number.parseFloat(eduObj.value))) {
            $(item).text(`${ ~~Number.parseFloat(eduObj.value)}万`.trim())
          } else if (eduObj && !Number.isFinite(Number.parseFloat(eduObj.value))) {
            let textValue = ``
            if(String(eduObj.value).length > 5) textValue = `${String(eduObj.value).substr(0,5)}...`
            else textValue = String(eduObj.value)
            $(item).text(`${textValue}`)
          } else {
            $(item).text(`**万`)
          }
        } catch (err) {}
      })

      $('.shutdown_this').each((index, item) => {
        if ($(item).attr('inori-state')) {
          $(item).find('div').text(`${$(item).attr('inori-state') === '0' ? '开启': '关停'}`)
        }
        $(item).on('click' , (evt) => {
          evt.preventDefault()
          evt.stopPropagation()
          $.confirm(
            "是否确定修改聊天室状态", '确定修改？',
            () => {
              // 点击确定后回调函数
              const state = $(evt.target).closest('.shutdown_this').attr('inori-state')
              const stateID = $(evt.target).closest('.shutdown_this').attr('inori-state-id')
              if (state === '' || state === null || state === undefined) {
                $(evt.target).find('div').remove()
                throw new Error('取消修改状态操作，聊天室状态为空')
              }
              if (stateID === '' || stateID === null || stateID === undefined) {
                $(evt.target).find('div').remove()
                throw new Error('取消修改状态操作，聊天室ID为空')
              }
              const ajaxURL = String($(`#ajax_state_chat`).val()).replace(/\s/g, '')
              const ajaxData = {
                state: state === '1' ? 0 : 1,
                chat_id: stateID
              }
              $.ajax(ajaxURL, {
                method: 'post',
                data: ajaxData,
                beforeSend: () => {
                  $.toptip(`正在发送请求，等待服务器响应中。。。`, 'success')
                }
              }).fail((err) => {
                $.toptip(`修改失败！请检查网络重试`, 'error')
              }).done((res) => {
                if (!res.code) {
                  $.toptip(`修改失败！服务器返回异常: ${JSON.stringify(res)}`, 'error')
                  throw new Error(`修改失败！服务器返回异常: ${JSON.stringify(res)}`)
                }
                if (res.code && String(res.code) !== '1') {
                  $.toptip(`修改失败！服务器返回异常: ${res.message}`, 'error')
                  throw new Error(`修改失败！服务器返回异常: ${res.message}`)
                }
              }).done((res) => {
                $.toptip(`修改成功`, 'success')
                const text = $(evt.target).closest('.shutdown_this').attr('inori-state') === '0' ? '关停': '开启'
                const attr = $(evt.target).closest('.shutdown_this').attr('inori-state') === '0' ? '1': '0'
                $(evt.target).closest('.shutdown_this').find('div').text(text)
                $(evt.target).closest('.shutdown_this').attr('inori-state', attr)
              })
            }, function() {
              $.toptip(`已取消修改`, 'success')
              //点击取消后的回调函数
              return ;
            }
          )
          return false
        })
      })
    })
  </script>
</body>
</html>